package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.CheckGroup;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CheckGroupDao {
    @Select("<script>select * from t_checkgroup <if test=\"queryString!=null and queryString!=''\">where CODE=#{queryString} or NAME=#{queryString} or HELPCODE=#{queryString}</if></script>")
    Page<CheckGroup> selectByCondition(@Param("queryString") String queryString);

    @Insert("insert into t_checkgroup(id,code,name,helpCode,sex,remark,attention) values(#{id},#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", keyColumn = "id", resultType = Integer.class)
    void add(CheckGroup checkGroup);

    @Insert("insert into t_checkgroup_checkitem(checkgroup_Id, checkitem_Id) values(#{checkgroupId}, #{checkitemId})")
    void setCheckGroupAndCheckItem(@Param("checkgroupId") Integer checkgroupId, @Param("checkitemId") Integer checkitemId);

    @Select("select checkitem_Id from t_checkgroup_checkitem where checkgroup_Id=#{checkgroupId}")
    List<Integer> findCheckItemIdsByCheckGroupId(@Param("checkgroupId") Integer checkgroupId);

    @Update("update t_checkgroup set code=#{code},name=#{name},helpCode=#{helpCode},sex=#{sex},remark=#{remark},attention=#{attention} where id=#{id}")
    void edit(CheckGroup checkGroup);

    @Delete("delete from t_checkgroup_checkitem where checkgroup_Id=#{checkgroupId}")
    void deleteAssocication(Integer checkgroupId);

    @Select("select * from t_checkgroup")
    List<CheckGroup> findAll();

    //==========================================================================================================================
    @Select("select * from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{setmealId})")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "checkItems", column = "id", many = @Many(select = "com.itheima.dao.CheckItemDao.findCheckItemByCheckGroupId"))
    })
    List<CheckGroup> findCheckGroupsBySetmealId(Integer setmealId);
}
